package com.erp.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 获取某年某月的第一天和最后一天
 */
public class DateUtil {

    public static String getLastDayOfMonth(int year, int month) {
         Calendar cal = Calendar.getInstance();
         cal.set(Calendar.YEAR, year);
         cal.set(Calendar.MONTH, month-1);
         cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DATE));
        return  new   SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
     }
     public static String getFirstDayOfMonth(int year, int month) {
         Calendar cal = Calendar.getInstance();
         cal.set(Calendar.YEAR, year);
         cal.set(Calendar.MONTH, month-1);
         cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE));
        return   new   SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
     }

    public static void main(String[] args) throws ParseException{
        System.out.println(getLastDayOfMonth(2021,9));
        System.out.println(getFirstDayOfMonth(2021,9));
        String date = getFirstDayOfMonth(2021,8);
        System.out.println(date);
    }
}